using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProgramMain.Framework;

namespace ProgramMain.Operators
{
    public partial class OperatorContractDetailCtl : BaseDetailCtl
    {
        public BindingSource phoneRoundDataSource;
        private bool m_bOnActionClose = false;

        public OperatorContractDetailCtl()
        {
            InitializeComponent();
        }

        public DialogResult ShowModal(bool IsEditable)
        {
            this.btnSave.Enabled = IsEditable;

            return frmBaseModal.ShowModal(this, labelCaption.Text, this.btnSave, this.btnCancel);
        }

        override public void FrmCommon_Load(object sender, EventArgs e)
        {
            if (this.ParentForm != null)
            {
                this.ParentForm.FormClosed += new FormClosedEventHandler(this.FrmCommon_FormClosed);
                this.ParentForm.FormClosing += new FormClosingEventHandler(this.FrmCommon_FormClosing);
            }

            this.BindDataControls();
        }

        private void BindDataControls()
        {
            this.txtEditName.DataBindings.Add(new Binding("Text", this.phoneRoundDataSource, "Name"));
            this.textEditFirstMinTolerance.DataBindings.Add(new Binding("Text", this.phoneRoundDataSource, "FirstMinTolerance"));
            this.textEditSecondMinTolerance.DataBindings.Add(new Binding("Text", this.phoneRoundDataSource, "SecondMinTolerance"));
            this.textEditFreeTime.DataBindings.Add(new Binding("Text", this.phoneRoundDataSource, "FreeTime"));
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!dxValidationProvider1.Validate())
            {
                foreach (Control c in dxValidationProvider1.GetInvalidControls())
                {
                    DevExpress.XtraEditors.DXErrorProvider.ValidationRuleBase rule =
                        dxValidationProvider1.GetValidationRule(c);
                    XtraMessageBox.Show(rule.ErrorText);
                    c.Focus();
                    return;
                }
            }

            this.phoneRoundDataSource.EndEdit();

            m_bOnActionClose = true;

            if (this.ParentForm != null)
                this.ParentForm.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            m_bOnActionClose = true;

            this.phoneRoundDataSource.CancelEdit();

            if (this.ParentForm != null)
                this.ParentForm.Close();
        }

        public void FrmCommon_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!m_bOnActionClose)
            {
                m_bOnActionClose = true;

                this.phoneRoundDataSource.CancelEdit();
            }
        }

        public void FrmCommon_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.ParentForm != null)
            {
                e.Cancel = this.ParentForm.DialogResult != DialogResult.Cancel
                    && !dxValidationProvider1.Validate();
            }
        }
    }
}
